import os
import struct
import random
import string
import tkinter as tk
from tkinter import filedialog

import socket

def stringlength_extend_256(starttext,frontorbehind):#将字符串扩展到256个
	slength=len(starttext)
	if slength>=256:
		return starttext
	i=256-slength
	endtext=starttext
	if frontorbehind==0:
		while i>0:
			endtext=endtext+b' '
			i-=1
	else:
		while i>0:
			endtext=b' '+endtext
			i-=1
	return endtext

def getfilename_from_string256(filenametext):#把后部有空格的256字符的字符串，提取出前面的非空格部分
	filename=''
	i=0
	while i<256 and filenametext[i]!=' ':
		filename=filename+filenametext[i]
		i+=1
	return filename

def randomstring(textlength):#生成一个随机的字符串，但是纯英文
	result=""
	for i in range(0,textlength):
		c=os.urandom()
		result=result+str(chr(c))
	return result

def stringadd_samelength(text1,text2):#两个纯英文的字符串逐字符模加
	if len(text1)!=len(text2):
		return ""
	else:
		length=len(text1)
		text3=""
		for i in range(0,length):
			c=(ord(text1[i])+ord(text2[i]))%126
			text3=text3+str(chr(c))
		return text3

def stringsub_samelength(text1,text2):#两个纯英文的字符串逐字符模减
	if len(text1)!=len(text2):
		return ""
	else:
		length=len(text1)
		text3=""
		for i in range(0,length):
			c=(ord(text1[i])-ord(text2[i]))%126
			text3=text3+str(chr(c))
		return text3